package com.hzy.servlet;

import com.hzy.pojo.Booking;
import com.hzy.service.BookingService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;


/**
 * @Package: servlet
 * @ClassName: UpdateServlet
 * @Author: 862982795
 * @CreateTime: 2021/06/02 10:50
 * @Description:
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        BookingService service = new BookingService();

        String userName = request.getParameter("userName");
        String  cardId= request.getParameter("cardId");
        Integer status = Integer.parseInt(request.getParameter("status"));
        Integer id = (Integer) request.getSession().getAttribute("id");
        String bookingDate = request.getParameter("bookingDate");


        Booking booking = new Booking();
        booking.setUserName(userName);
        booking.setCardId(cardId);
        booking.setBookingDate(Date.valueOf(bookingDate));
        booking.setStatus(status);
        booking.setId(id);

        int i = service.updateBooking(booking);

        PrintWriter out = response.getWriter();
        if (i > 0) {
            out.println("<script>alert('修改成功');location.href='ListServlet';</script>");
        } else {
            out.println("<script>alert('修改失败!');history.back();</script>");
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
